//
//  CommentPage.m
//  TodayComment
//
//  Created by 张吉阳 on 15/9/12.
//  Copyright (c) 2015年 xianrenmoshi. All rights reserved.
//

#import "CommentPage.h"
#import "ColumnInfo.h"
#import "FxGetNews.h"
#import "CommentLandscapeCell.h"
#import "WeatherPage.h"
#import "FxDBManager.h"
#import "CollectionPage.h"
#import "FxClearChache.h"
#import "DKNightVersion.h"
#import "PCStackMenuItem.h"
#import "PCStackMenu.h"
#import "MyPage.h"
#import "NightModeSingle.h"
@implementation CommentPage

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavigationRight:@"NavigationSquare.png"];
    [self setNavigationLeft:@"NavigationSquare.png" sel:@selector(settingButton)];
    NightModeSingle *nightMode = [NightModeSingle nightModeShare];
    nightMode.nightTag = @"content_template2";
    _tableView.gapBetweenCells = 2;
    _tableView.cellsToPreload = 1;
    _tableView.currentCellIndex = -1;
    [self addSegmentedControl];
    

}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}



- (void)settingButton
{
    MyPage *page = [[MyPage alloc]init];
    [self.navigationController pushViewController:page animated:YES];
}


#pragma mark - AddSegmentedControl Method
- (void)addSegmentedControl
{
    NSArray *segArray = [NSArray arrayWithObjects:@"今日", @"评论", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segArray];
    segmentedControl.selectedSegmentIndex = 0;
    [segmentedControl setWidth:60 forSegmentAtIndex:0];
    [segmentedControl setWidth:60 forSegmentAtIndex:1];
    self.navigationItem.titleView = segmentedControl;
}

#pragma mark - Add

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_barWidget == nil) {
        [self addBarWidget];
    }
}

- (void)addBarWidget
{
    _barWidget = [[ColumnBarWidget alloc] init];
    _barWidget.delegate = self;
    _barWidget.view.frame = _barBackView.bounds;
   _barWidget.view.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    [_barBackView addSubview:_barWidget.view];
    [_barBackView sendSubviewToBack:_barWidget.view];
    [self.tabBarController.view addSubview:_tabBar];
}





- (void)didSelect:(NSInteger)pageIndex
{
    if (_tableView.currentCellIndex != pageIndex) {
        _tableView.currentCellIndex = pageIndex;

        [_tableView reloadData];
    }
}

- (void)leftDidSelect
{
    
    if ( _tableView.currentCellIndex == 0) {
        _tableView.currentCellIndex = _barWidget.listDataPro.count - 1;
    }
    else{
        _tableView.currentCellIndex --;
    }
        
   [_tableView reloadData];
    
}

- (void)rightDidSelect
{
    
    if ( _tableView.currentCellIndex == _barWidget.listDataPro.count - 1) {
        _tableView.currentCellIndex = 0;
    }
    else{
        _tableView.currentCellIndex ++;
    }
    
    [_tableView reloadData];
    
}


- (void)tabBar:(FxTabBar *)tabBar didSelectIndex:(NSInteger)index
{
    
       if (index == 0) {
           [self leftDidSelect];
       }
      NSString *path = [NSString stringWithFormat:@"%@/%@", [FxGlobal getRootPath], CacheImagePath];
      double fileSize = [FxFileUtility calculteFileSzieEx:path]/1024.0/1024.0;
      NSString *fileSizeName = [NSString stringWithFormat:@"%@%.0f%@", @"清除缓存", fileSize, @"兆"];
        if (index == 1) {
            [PCStackMenu showStackMenuWithTitles:
             [NSArray arrayWithObjects: @"我的收藏", fileSizeName,
              @"夜间模式", nil]
                                      withImages:[NSArray arrayWithObjects: [UIImage imageNamed:@"magnifier@2x.png"],
                                                  [UIImage imageNamed:@"twitter@2x.png"],
                                                  [UIImage imageNamed:@"speech@2x.png"],
                                                nil]
                                    atStartPoint:CGPointMake(tabBar.center.x + 10, tabBar.frame.origin.y - tabBar.frame.size.height - 20)
                                          inView:self.view
                                      itemHeight:40
                                   menuDirection:PCStackMenuDirectionClockWiseUp
                                    onSelectMenu:^(NSInteger selectedMenuIndex) {
                                        if (selectedMenuIndex == 0) {
                                            CollectionPage *page = [[CollectionPage alloc]init];
                                            [self.navigationController pushViewController:page animated:YES];
                                        }
                                        if (selectedMenuIndex == 1) {
                                            NSString *path = [NSString stringWithFormat:@"%@/%@", [FxGlobal getRootPath], CacheImagePath];
                                            [FxClearChache clearChache:path];
                                        }
                                        if (selectedMenuIndex == 2) {
                                            [self change];
                                        }
                                        
                                    }];
            
        }
    if (index == 2) {
        [self rightDidSelect];
    }
    
}

- (void)change {
     NightModeSingle *nightMode = [NightModeSingle nightModeShare];
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
        nightMode.nightTag = @"content_template2";
        [DKNightVersionManager dawnComing];
    } else {
        nightMode.nightTag = @"content_template3";
        [DKNightVersionManager nightFalling];
    }
}


#pragma mark - FxLandscapeViewDataSource & FxLandscapeViewDelegate methods

- (NSInteger)numberOfCellsInTableView:(FxLandscapeTableView *)tableView
{
    return _barWidget.listDataPro.count;
    
}

- (FxLandscapeCell *)cellInTableView:(FxLandscapeTableView *)tableView atIndex:(NSInteger)index
{
    CommentLandscapeCell *cell = (CommentLandscapeCell *)[tableView dequeueReusableCell];
    
    if (cell == nil) {
        cell = [[CommentLandscapeCell alloc] initWithFrame:_tableView.bounds];
        cell.owner = self;
    }
    
    ColumnInfo *info = [_barWidget.listDataPro objectAtIndex:index];
    [cell setCellData:info];
    
    return cell;
}

- (void)tableView:(FxLandscapeTableView *)tableView didChangeAtIndex:(NSInteger)index
{
    _barWidget.pageIndex = index;
}

- (IBAction)doRight:(id)sender
{
    WeatherPage *page = [[WeatherPage alloc]init];
    
    [self.navigationController pushViewController:page animated:YES];
}

@end

